package com.leo;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.Comparator;

/**
 * @ClassName ListViewSort
 * @Description
 * @Author wangss
 * @date 2020.06.11 23:57
 * @Version 1.0
 */
public class ListViewSort extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Button sort = new Button("排序");

        ListView<String> listView = new ListView();
        listView.setPrefWidth(400d);
        listView.setPrefHeight(400d);

        ObservableList<String> obsList = listView.getItems();
        obsList.add("123");
        obsList.add("4561");
        obsList.add("4562");
        obsList.add("1234");
        obsList.add("4565");
        obsList.add("456f");
        obsList.add("456d");

        anchorPane.getChildren().addAll(sort, listView);
        AnchorPane.setTopAnchor(sort, 100d);
        AnchorPane.setLeftAnchor(sort, 100d);
        AnchorPane.setTopAnchor(listView, 140d);
        AnchorPane.setLeftAnchor(listView, 100d);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("搜索功能");
        primaryStage.setResizable(false);
        primaryStage.show();

        sort.setOnAction(event -> listView.setItems(obsList.sorted(Comparator.comparing(String::length).reversed())));
    }
}
